<?php

namespace Cktadmin\Controller;

use Common\Controller\BasicAdmin;
use Common\Service\DataService;

class UserController extends BasicAdmin {

    public $table = 'user';

    public function index() {
        $this->title = '会员管理';
        $where = array();
        $get = I('get.');
        (isset($get['key']) && $get['key'] !== '') && $where['email|nick|name|mobile|qq'] = array('like', "%{$get['key']}%");
        $count = M($this->table)->where($where)->count();
        $db = M($this->table)->where($where)->order('add_time desc');
        return parent::_list($db, $count);
    }

    public function edit() {
        return parent::_form($this->table, 'form');
    }

    public function _edit_form_filter(&$vo) {
        if (IS_POST && $vo['password'] != '') {
            $vo['password'] = md5($vo['email'] . $vo['password']);
        } else {
            unset($vo['password']);
        }
    }

    public function del() {
        if (DataService::update($this->table)) {
            $this->success('删除成功！');
        }
        $this->error('删除失败！');
    }

    public function forbid() {
        if (DataService::update($this->table)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    public function resume() {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

}
